package com.flux.models.tasks;

import com.common.bus.task.BaseNetworkRunnable;
import com.common.bus.task.ICallBack;
import com.flux.models.bean.EventInfo;
import com.flux.models.bean.EventsRequest;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.List;

import okhttp3.Response;

/**
 * Created by chenxunlin01 on 2017/10/12.
 */

public class FetchEventsRunnable extends BaseNetworkRunnable<List<EventInfo>> {

    private final EventsRequest request;
    private ICallBack callBack;

    public FetchEventsRunnable(EventsRequest request) {
        this.request = request;
        callBack = null;
    }

    public FetchEventsRunnable(EventsRequest request,ICallBack callBack) {
        this.request = request;
        this.callBack = callBack;
    }

    @Override
    public void onPreTraktCall() {
        if(callBack!=null){
            callBack.onPreTraktCall();
        }
    }

    @Override
    public List<EventInfo> doBackgroundCall() throws Exception {
        Response response = OkHttpUtils.postString().url("http://api.vancoolver.ca/2.0.1/").content(new Gson().toJson
                (request)).build()
                .execute();
        return handleHttpResponse(response);
    }

    @Override
    public void onSuccess(List<EventInfo> result) {
        if(callBack!=null){
            callBack.onSuccess(result);
        }
    }

    @Override
    public void onError(Exception re) {
        if(callBack!=null){
            callBack.onError(re);
        }
    }
}
